package com.ychong.library.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.ychong.library.R
import com.ychong.library.databinding.DialogBottomPlateKeyboardBinding
import com.ychong.library.view.YCHPlateKeyBoardView

class BottomPlateKeyBoardDialog (private var mContext: Context) :
    Dialog(mContext, R.style.dialog_bottom_full), View.OnClickListener {
        private var binding:DialogBottomPlateKeyboardBinding?=null
    private var keyboardListener: KeyBoardListener? = null
    public fun setKeyBoardListener(listener: KeyBoardListener):BottomPlateKeyBoardDialog{
        this.keyboardListener = listener
        return this
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //取消图层（解决弹窗后状态栏颜色改变问题）
        window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        binding =
            DataBindingUtil.inflate(layoutInflater, R.layout.dialog_bottom_plate_keyboard, null, false)
        setContentView(binding!!.root)
        val window = window
        window!!.setGravity(Gravity.BOTTOM)
        window.setWindowAnimations(R.style.share_animation)
//        //取消图层（解决弹窗后状态栏颜色改变问题）
//        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        window.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        initListener()
    }
    private fun initListener(){
        binding!!.completeTv.setOnClickListener(this)
        binding!!.resetTv.setOnClickListener(this)

        binding!!.ychPlateKeyBoardView.setOnKeyBoardListener(object : YCHPlateKeyBoardView.OnKeyBoardListener{
            override fun click(text: String) {
                keyboardListener?.input(text)
            }

            override fun remove() {
                keyboardListener?.remove()
            }
        })
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.complete_tv -> {
                keyboardListener!!.complete()
                dismiss()
            }
            R.id.reset_tv -> {
                keyboardListener!!.clear()
            }
        }
    }
    fun build():BottomPlateKeyBoardDialog{
        show()
        return this
    }


    interface KeyBoardListener{
        fun input(num:String)
        fun remove()
        fun clear()
        fun complete()
    }
}